﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;

public partial class Manager_Ads_Ads : System.Web.UI.Page
{
    protected string pDel_mk = "", pUpd_mk = "";
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["AdminUserID"] == null)
        {
            Response.Write("<script language=javascript>alert('Time is out.');window.open('../Default.aspx','_top');</script>");
            return;
        }
        clsGetMenuAuth clsAuth = new clsGetMenuAuth(Session["AdminUserID"].ToString(), Session["AutID"].ToString(), Session["IsAdmin"].ToString(), Request.QueryString["CodeID"]);
        if (clsAuth.IsDeleteAuth() == true)
            pDel_mk = "Y";
        if (clsAuth.IsUpdateAuth() == true)
            pUpd_mk = "Y";
        if (!IsPostBack)
        {
            if (clsAuth.IsAddAuth() == false)
                btnAdd_Catalogue.Visible = false;
            txtLink.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + btnSearch.ClientID + "').click();return false;}} else {return true}; "); 
            txtName.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + btnSearch.ClientID + "').click();return false;}} else {return true}; "); 

            try
            {
                ddlKindNo.SelectedItem.Selected = false;
                ddlKindNo.Items.FindByValue(Request.QueryString["KindNo"]).Selected = true;
            }
            catch { }
            BindData();
        }
    }
    private void BindData()
    {


        PccMsg msg = new PccMsg();

        
        msg.CreateFirstNode("AdsID", "0");
        msg.CreateFirstNode("AdsNm", txtName.Text);
        msg.CreateFirstNode("AdsLink", txtLink.Text);
        msg.CreateFirstNode("KindNo", ddlKindNo.SelectedValue);

        clsWebShop cls = new clsWebShop();
        DataSet ds = null;
        ds = cls.Get_AdsByCondition(msg.GetXmlStr);
        grv_RSS.DataSource = ds;
        grv_RSS.DataBind();

    }

    protected void grv_RSS_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[1].Attributes.Add("onclick", "if(confirm('Delete???')) return true; return false;");
            //Truong hop xoa
            if (pDel_mk != "Y")
            {
                e.Row.Cells[1].Text = "";
                e.Row.Cells[1].Attributes.Add("onclick", "javascript:return false;");
            }
            //Truong hop Update
            if (pUpd_mk != "Y")
            {
                e.Row.Cells[0].Text = "";
                e.Row.Cells[0].Attributes.Add("onclick", "javascript:return false;");
            }
            if (DataBinder.Eval(e.Row.DataItem, "AdsType").ToString().ToUpper() == "IMG")
            {
                HtmlAnchor embed = new HtmlAnchor();
                embed.InnerHtml = "<img border='0' src='" + ResolveUrl("~/" + DataBinder.Eval(e.Row.DataItem, "AdsURL")) + "' height='80px' width='100px' >";
                e.Row.Cells[4].Controls.Add(embed);
            }
            else if (DataBinder.Eval(e.Row.DataItem, "AdsType").ToString().ToUpper() == "SWF")
            {
                HtmlAnchor embed = new HtmlAnchor();
                embed.InnerHtml = "<embed quality='high'   src='" + ResolveUrl("~/" + DataBinder.Eval(e.Row.DataItem, "AdsURL")) + "'  loop='false' height='80px'  width='100px' pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash'> </embed>";
                e.Row.Cells[4].Controls.Add(embed);
            }
        }
    }
    protected void btnAdd_Catalogue_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddAds.aspx?KindNo=" + Request.QueryString["KindNo"] + "&CodeID=" + Request.QueryString["CodeID"]);
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindData();
    }
    protected void grv_RSS_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
      

        Label lblAdsURL = (Label)grv_RSS.Rows[e.RowIndex].FindControl("lblAdsURL");

        System.IO.FileInfo file = new System.IO.FileInfo(Server.MapPath("~") + "/" + lblAdsURL.Text);
        if (file.Exists)
            file.Delete();

        clsWebShop cls = new clsWebShop();
        cls.UpdateData("delete from Ads where AdsID=" + grv_RSS.DataKeys[e.RowIndex].Value.ToString());

        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Dữ liệu đã cập nhật thành công')</script>");
        BindData();
    }


    protected void grv_RSS_RowCreated(object sender, GridViewRowEventArgs e)
    {
        for (int i = 0; i < grv_RSS.Rows.Count - 1; i++)
        {
            System.Web.UI.WebControls.WebControl myControl = grv_RSS.Rows[i].Cells[1];
            myControl.Attributes.Add("onclick", "if(confirm('Delete???')) return true; return false;");
        }
    }
    protected void ddlKindNo_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindData();
    }
    protected void grv_RSS_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grv_RSS.PageIndex = e.NewPageIndex;
        BindData();
    }
}
